<?php
require_once('lib.php');

// устанавливаем параметры
$dtype     = required_param('dtype', PARAM_TEXT);

if ( $dtype == 'files')
{
    $ftypes    = required_param('ftypes', PARAM_TEXT);
    $fsize     = required_param('fsize', PARAM_INT);
}

print_object($dtype);
print_object($ftypes);
print_object($fsize);

// проверка доступа
require_capability("tool/cleanup:access", get_system_context());

admin_externalpage_setup('toolcleanup');
echo $OUTPUT->header();

// установим параметр-подтверждение
$isdelete = optional_param('delete', '', PARAM_TEXT);

if ( $isdelete == 'yes' )
{// удаление подтверждено
    if ($dtype == 'files')
    {
        if( delete_deprecated_files(explode(' ', $ftypes), intval($fsize)) )
        {
            echo "<h4 align='center' style='color:green;'>".get_string('removal_success', 'tool_cleanup')."</h4>";
        }
    }else if ( $dtype == 'users')
    {
        if(delete_inactive_users() )
        {
            echo "<h4 align='center' style='color:green;'>".get_string('removal_success', 'tool_cleanup')."</h4>";
        }
    }
}else
{
    if ($dtype == 'files')
    {// выведем диалог для подтверждения
        $linkyes = new moodle_url($CFG->wwwroot.'/admin/tool/cleanup/delete.php?delete=yes', array(
                'dtype' => $dtype, 'ftypes' => $ftypes, 'fsize' => $fsize));
        $linkno  = new moodle_url($CFG->wwwroot.'/index.php');
        $msg  = "<h4>".get_string('confirm_delete', 'tool_cleanup')."</h4>";
        $msg .= "<p>".get_string('action_delete_files', 'tool_cleanup')."</p>";
        $msg .= "<p>".get_string('extensions_list', 'tool_cleanup').": ".$ftypes."</p>";
        $msg .= "<p>".get_string('min_remove_size', 'tool_cleanup').": ".$fsize."</p>";
        echo $OUTPUT->confirm($msg, $linkyes, $linkno);
        
    }else if ($dtype == 'users')
    {// выведем диалог для подтверждения
        $linkyes = new moodle_url($CFG->wwwroot.'/admin/tool/cleanup/delete.php?delete=yes', array(
                'dtype' => $dtype));
        $linkno  = new moodle_url($CFG->wwwroot.'/index.php');
        $msg  = "<h4>".get_string('confirm_delete', 'tool_cleanup')."</h4>";
        $msg .= "<p>".get_string('action_delete_inactive_users', 'tool_cleanup')."</p>";
        echo $OUTPUT->confirm($msg, $linkyes, $linkno);
    }
}

echo $OUTPUT->footer();

?>